/*
 * 标准开发模式下，使用Restfull API进行开发的范例 
 * 
 * @author yangw
 * @since 1.0.0
 */
'use strict';

standard.controller('RestfullCtrl', function($scope, $templateCache, $modal, restfull, util) {	
	
	$templateCache.put('modal.html','<div ng-include="'+"'/sweeter/resources/views/standard/modal.html'"+'"></div>');
	$scope.orgcolor = ['primary', 'info', 'success', 'warning', 'danger', 'dark'];
	$scope.org = '';
	
	/*加载组织部门列表*/	
	restfull.query('/standard/orglist', {}, function(resp) {
    	$scope.orglist = resp.datas.orglist;
    });
			
	/*加载用户员工列表*/
	$scope.loadGrid = function(){
		restfull.query('/standard/userlist', {userOrgId:$scope.org.orgId}, function(resp) {
			$scope.userlist = resp.datas.userlist;
	    	setTimeout(function () {//留给angularjs足够的渲染时间
	    		$('table tbody').trigger('footable_redraw');
	        }, 100);
	    });  
	};
	$scope.loadGrid();
	
	/*新增或编辑对话框*/
	$scope.open = function(type) {		
		if('Add' != type && $('tbody input:visible:checked').size() != 1){
    		util.warning('Warning','only one item should be checked.');
    		return;
    	}
		$modal.open({backdrop : 'static', templateUrl : 'modal.html', controller : 'StandardModalCtrl',
			resolve : {
				type : function() { return angular.copy(type);  },
				orglist : function() { return $scope.orglist; }
			}
		});
	};
	
	/*删除用户*/
	$scope.del = function() {		
		var choises = $('tbody input:visible:checked');		
    	if(choises.size() < 1){
    		util.warning('Warning','at least one item should be checked.');
    	}else {   		
    		util.confirm("do you realy want to delete these items?", function(){
    			var data = $.map(choises.serializeArray(), function(n){
	  				  return n.value;
	  			});
	  			restfull.del('user', data, function(resp){
	  				$('table').data('footable').removeRow(choises.closest('tr'));//刷新数据
	  			});
    		});   		
    	}
	};
	
	/*通过组织查询用户*/
	$scope.selectOrg = function(org) {		
		if($scope.org == org){
			$scope.org = ''; 
			org.selected = false;
		}else{
			angular.forEach($scope.orglist, function(org) {
				org.selected = false;
			});
			$scope.org = org; 
			$scope.org.selected = true;
		}
		$scope.userlist = null; //加载等待效果
		$scope.loadGrid();
	};
	
	/*全选/反选*/
	$("thead .i-checks i").click(function(){
		$("tbody .i-checks i").trigger("click");    
    }); 
	 
});

standard.controller('StandardModalCtrl', function($scope, $rootScope, $state, $modalInstance, 
		restfull, BeanlistService, type, orglist) {
	$scope.type = type;
	$scope.orglist = orglist;
	$scope.user = {userOrgId:"10006"};

	if('Add' != type){
		restfull.get('user', $('tbody input:visible:checked').val(), function(resp){
			$scope.user = resp.datas.result;
			$scope.user.userRole = resp.datas.result.userRole==3?false:true;
			$scope.userResource = resp;
		});
	}
		
	$scope.ok = function() {
		if('Add' == type){			
			restfull.save('/entity/user', BeanlistService.getPostData($scope.user), function(resp){
				$state.reload('restfull');
				$modalInstance.close();
			});
		}else {			
			restfull.update($scope.userResource, BeanlistService.getPutData($scope.user), function(resp){
				$state.reload('restfull');
				$modalInstance.close();
			});
		}			
	};

	$scope.cancel = function() {
		$modalInstance.dismiss('cancel');
	};
});